#include "p24FJ256GB110.h"
#include "Mikro.h"
#include "MikroSPI.h"

/* Write a byte to MOSI and read a byte from MISO */
uint8 SPIwrite(uint8 data)
{
	SPI2BUF = data;		// fill TX buffer
	while (!(SPI2STAT & 0x0080));	// wait for SRMPT set
	return SPI2BUF;		// read RX buffer
}

/* SPI module 2 initialization */
void SPI_Init()
{
	// Map pins 
	_RP21R = 11;		// Remap RG6 to SPI2CLK
	_RP19R = 10;		// Remap RG8 to SPI2DATA OUT
	_SDI2R = 26;		// Remap RP26 to SPI2DATA IN
	_TRISG6 = 0;		// RG6 output for SPI2CLK
	_TRISG7 = 1;		// RG7 input for SPI2 DATA IN
	_TRISG8 = 0;		// RG8 output for SPI2 DATA OUT

	IFS2bits.SPI2IF = 0;	// Clear interrupt flag
	IEC2bits.SPI2IE = 0;	// Disable interrupts

	SPI2CON1bits.MODE16 = 0;	// Send 8 bits at a time
	SPI2CON1bits.DISSCK = 0;	// Enable internal spi clock 
	SPI2CON1bits.DISSDO = 0;	// SDO2 pin is controlled by module
	SPI2CON1bits.SMP = 0;	// Sample middle of data output time
	SPI2CON1bits.CKE = 1;	// Changes on active to idle
	SPI2CON1bits.CKP = 0;	// Clock active high
	SPI2CON1bits.MSTEN = 1;	// Enable master mode

	SPI2CON2bits.FRMEN = 0;	// Disable frame support
	SPI2CON2bits.SPIBEN = 1;	// Enable enhanced buffer mode

	SPI2STATbits.SPIROV = 0;	// Clear SPIROV

	SPI2STATbits.SPIEN = 1;	// Enable module
}

void SPI2_Clock_Speed(uint8 pri_prescale, uint8 sec_prescale)
{
	/* Reference for SPI clock speed settings
	 *   _____________________________Secondary Prescalar_________________
	 *   FCY = 16 MHz______|______|__1:1__|__2:1__|__4:1__|__6:1__|__8:1__|
	 *   Primary Prescalar |   1:1|Invalid|   8000|   4000|   2667|   2000|
	 *                     |   4:1|   4000|   2000|   1000|    667|    500|
	 *                     |  16:1|   1000|    500|    250|    167|    125|
	 *                     |  64:1|    250|    125|     63|     42|     31|
	 *   _________________________________________________________________
	 *   FCY =  5 MHz______|______|__1:1__|__2:1__|__4:1__|__6:1__|__8:1__|
	 *   Primary Prescalar |   1:1|   5000|   2500|   1250|    833|    625|
	 *                     |   4:1|   1250|    625|    313|    208|    156|
	 *                     |  16:1|    313|    156|     78|     52|     39|
	 *                     |  64:1|     78|     39|     20|     13|     10|
	 *
	 * (SCKx frequencies shown in kHz)
	 */
	SPI2STATbits.SPIEN = 0;	// Disable module

	SPI2CON1bits.PPRE = pri_prescale;
	SPI2CON1bits.SPRE = sec_prescale;

	SPI2STATbits.SPIEN = 1;	// Enable module
}
